import * as fileUtils from './files.js'
import * as utils from './utils.js'
const {
    showMessage
} = utils
const { getBase64ImageByUrl } = fileUtils
const extension = ['jpg', 'jpeg', 'image']




module.exports = {
    showMessage,
    chooseImage(self) {
        return new Promise((r, j) => {
            uni.chooseImage({
                extension,
                count: 1,
                sizeType: 'compressed',
                success: (e) => {
                    const { tempFilePaths, tempFiles } = e;
                    if (tempFilePaths.length) {
                        getBase64ImageByUrl.call(self, tempFilePaths[0], self).then(({ base64, success }) => {
                            if (success) {
                                const fl = tempFilePaths[0].split('.');
                                if (extension.includes(fl[fl.length - 1])) {
                                    const sp = base64.split(',');

                                    r({ base64: sp[1] || sp[0], success: true });
                                } else {
                                    j({
                                        success: false,
                                        msg: '选取图片失败'
                                    })

                                    showMessage('只支持' + extension.join('、') + '格式的图片');
                                }
                            } else {
                                j({
                                    success,
                                    msg: '选取图片失败'
                                })
                            }
                        })
                    }
                },

                fail() {
                    showMessage('取消选择');
                    j({
                        msg: '取消选择',
                        success: false
                    })
                }
            });
        })
    },

    /* 
        {
          title: '输入框',
          type: 'input-text',
          value: '',
          placeholder: '上传证件自动填写',
          key: 'key1',

          emptyMsg: '值不能为空',
          required: true,

          reg: /^[a-zA-Z]{0,}$/.toString(), // 与 regMsg 配合(以字符串形式传入， 特殊符号要有转义符:  ^\\s*$), 只有value值是字符串时候有效
          regMsg: '正则校验失败',
          warningType: 'model' , // model | toast(default)  出错提示
        }
    */
    checkVaild(list) {
        const len = list.length >>> 0;
        let k = 0;
        let flag = true;
        let err = null;
        let warningType1 = '';
        let errItem = null;


        while (k < len) {
            const o = list[k++]
            const { value, required, emptyMsg, regMsg, warningType, highReg, highRegMsg } = o;
            let { reg } = o;

            const ty = typeof value;
            warningType1 = warningType;

            if (required) {
                if (['object', 'string'].includes(ty)) {
                    const len1 = value.length;

                    // 验证非空
                    if (!len1) {
                        flag = false;
                        err = emptyMsg;
                        errItem = o;
                        break;
                    }
                }
            }

            // 校验正则
            if (reg && ty === 'string' && value) {
                const regType = typeof reg;
                let regInstance = null;

                // 非h5 环境传入的reg只能是字符串形式
                if (regType === 'string') {
                    if (/^\/\S*\s*\/$/.test(reg)) {
                        reg = reg.slice(1, reg.length - 1);
                    }

                    regInstance = new RegExp(reg);
                } else {
                    regInstance = reg;
                }

                if (!regInstance.test(value)) {
                    err = regMsg;
                    flag = false;
                    errItem = o;

                    break;
                }
            }

            if (highReg && ty === 'string' && value) {
                // 严格测试
                highReg.replace(/\/\//g, '/');
                if (/^\/\S*\s*\/$/.test(highReg)) {
                    highReg = highReg.slice(1, highReg.length - 1);
                }
                const higRegInstance = new RegExp(highReg);
                const sRegRes = higRegInstance.exec(value);
                console.log(higRegInstance, sRegRes);
                if (sRegRes) {
                    if (sRegRes.input !== sRegRes[0]) {
                        err = highRegMsg;
                        flag = false;
                        errItem = o;
                        break;
                    }
                } else {
                    err = highRegMsg;
                    flag = false;
                    errItem = o;
                    break;
                }
            }
        }
        if (!flag) {
            console.log(errItem, ', 出错了----233');

            if (warningType1 === 'model') {
                uni.showModal({
                    title: '提示',
                    // content: JSON.stringify(res),
                    content: err,
                    showCancel: false,
                    confirmText: '确定',
                    complete: () => {},
                });
            } else {
                showMessage(err || errItem.title);
            }
        }

        return flag;
    },
}